package local.pt.iscte.dcti.pa.utils;

import java.util.List;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Defines the common class
 * @author Simão Fernandes
 * @version %I%, %G%
 * @since 1.0
 */
public class Common {
	//todos os produtos
    //[\+\-]?([A-Z]+\d+)([\*/]([A-Z]+\d+))+


    //todas as somas (adicionar o +)
    //[\+\-][A-Z]+\d\d+(?![/\*])

    //com digitos
    //[\+\-]\d\d+(?![/\*])
    
    public static final String SUM = "\\+";
    public static final String SUBTRACTION = "\\-*";
    public static final String MULTIPLICATION = "\\*";
    public static final String DIVISION = "/";
    
    public static final String OPERATORS = "[" + SUM + SUBTRACTION + MULTIPLICATION + DIVISION + "]";
    public static final String PRECEDENT_OPERATORS = "[" + MULTIPLICATION + DIVISION + "]"; 
    public static final String SUBSEQUENT_OPERATORS = "[" + SUM + SUBTRACTION + "]";                
    
    public static final String ONE_OR_MORE_DIGITS = "\\d+"; 
    
    public static final String MORE_THAN_2_PRECEDENT_OPERATORS = ONE_OR_MORE_DIGITS + "(" + PRECEDENT_OPERATORS + ONE_OR_MORE_DIGITS + ")+" + PRECEDENT_OPERATORS + ONE_OR_MORE_DIGITS;     
    public static final String TWO_PRECEDENT_OPERATORS = ONE_OR_MORE_DIGITS + PRECEDENT_OPERATORS + ONE_OR_MORE_DIGITS;     
    
    public static final String ALPHANUMERIC_COORDINATE = "[A-Z]+" + ONE_OR_MORE_DIGITS;
    
    /**
     * Gets the letter for a given index
     * @param intIndex Index
     * @return Letter
     */
    public static String getColumnIndex(int intIndex) {
            String[] letters = new String[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
            
            
    String ans = "";
    while(intIndex>-1)
    {       
            ans = letters[intIndex%26]+ans;
            intIndex = intIndex/26 - 1;
    }
    return ans;
    }
    
    /**
     * Gets the index of a given letter
     * @param abscissa Letter
     * @return Index
     */
    public static int getColumnIndex(String abscissa) {
            for (int i = 0; i < 1024; i++) {
                    if (getColumnIndex(i).equals(abscissa)) {
                            return i;
                    }
            }
            return -1;
    }
    
    /**
     * Gets a list of matching strings with a given regular expression
     * @param regex Regular expression
     * @param characterSequence Character sequence to evaluate
     * @return Matching sequences
     */
    public static List<String> getMatches(String regex, String characterSequence) {
            
            Pattern patt = Pattern.compile(regex);
            Matcher mat = patt.matcher(characterSequence);
            
            List<String> matches = new ArrayList<String>();
            while (mat.find()) {
                    matches.add(mat.group());
            }
            
            return matches;
            
    }
    
    /**
     * Gets the numeric coordinates of a given alphanumeric coordinates
     * @param alphaNumCoordinates Alphanumeric coordinates
     * @return Coordinates
     */
    public static int[] GetCoordinates(String alphaNumCoordinates)
    {
            //abscissa
            List<String> matchResults = getMatches("[A-Z]+", alphaNumCoordinates);
            //TODO validar resultado
            String alphaAbscissa = matchResults.get(0);
            
            int[] coordinates = new int[2];
            coordinates[1] = getColumnIndex(alphaAbscissa); //abscissa
            
            matchResults = getMatches("\\d+", alphaNumCoordinates);
            //TODO validar resultado
            coordinates[0] = Integer.parseInt(matchResults.get(0)) - 1;
            
            return coordinates;
    }
    /**
     * Evaluates if a character sequence is a digit
     * @param s Evaluating character sequence
     * @return True if the character sequence is a digit, otherwise returns false
     */
    public static boolean isDigit(String s)
    {
            return s.matches("\\d+");
    }
}
